package com.wen.controller.biz;

import com.wen.common.utils.ResponseResult;
import com.wen.common.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
public class ExceptionController {
    /**
     * 重新抛出异常
     */
    @RequestMapping("/error/exthrow")
    public ResponseResult<?> rethrow(HttpServletRequest request) {
        CustomException ex = (CustomException)request.getAttribute("token.error");
        return ResponseResult.error(ex.getErrorCode(),ex.getErrorMsg() );
    }
    @RequestMapping("/500")
    public ResponseResult<?> ex500(HttpServletRequest request) {
       return ResponseResult.error("500","500Controller");
    }

    @RequestMapping("/unauthorized/{message}")
    public ResponseResult<?> unauthorized(@PathVariable("message") String message) {
       return ResponseResult.error("500",message);
    }

    @Test
    public void te() throws Exception{
        try {
            throw new NullPointerException();
        } catch (Exception e) {
            log.info("发生了[{}]异常",e.getClass());
        }
    }

}
